package com.apache.audit.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.audit.entity.LogModelDetail;
import com.apache.audit.manager.LogModelDetailManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class LogModelDetailManagerImpl implements LogModelDetailManager {

	protected IDao logModelDetailDao;
	protected final String entityName = "com.apache.audit.entity.LogModelDetail";

	public void setLogModelDetailDao(IDao logModelDetailDao) {
		this.logModelDetailDao = logModelDetailDao;
	}

	public String saveInfo(ParamsVo<LogModelDetail> vo) throws BusinessException {
		LogModelDetail logModelDetail = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="logModelDetail_"+infoId;
		logModelDetail.setDetailId(infoId);
		MethodParam param = new MethodParam("LogModelDetail", cacheKey, "", entityName);
		param.setVaule(logModelDetail);
		if (logModelDetailDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<LogModelDetail> vo) throws BusinessException {
		LogModelDetail logModelDetail = vo.getObj();
		if (Validator.isNotNull(logModelDetail.getDetailId())) {
			String cacheKey = "";
			//String cacheKey="logModelDetail_"+logModelDetail.getDetaulId();
			MethodParam param = new MethodParam("LogModelDetail", cacheKey, "", entityName);
			param.setVaule(logModelDetail);
			return logModelDetailDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<LogModelDetail> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="logModelDetail_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		LogModelDetail info = (LogModelDetail) logModelDetailDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("LogModelDetail");
			return logModelDetailDao.edit(param);
		} else {
			param.setParams("detailId", infoId);
			param.setDelete(true);
			return logModelDetailDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<LogModelDetail> vo) {
		String infoId = vo.getInfoId();
		String key = Validator.getDefaultStr(vo.getMethodKey(), "ById");
		String cacheKey = "";
		//String cacheKey="logModelDetail_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam(key, cacheKey, "", entityName);
		param.setInfoId(infoId);
		return logModelDetailDao.selectById(param);
	}

	public Object execute(ParamsVo<LogModelDetail> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<LogModelDetail> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = logModelDetailDao.pageSelect(param);
		return page;
	}

	public List<LogModelDetail> getList(ParamsVo<LogModelDetail> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return logModelDetailDao.select(param);
	}

	public long countInfo(ParamsVo<LogModelDetail> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return logModelDetailDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<LogModelDetail> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(logModelDetailDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		LogModelDetail logModelDetail = vo.getObj();
		if (!Validator.isEmpty(logModelDetail)) {
			if (Validator.isNotNull(logModelDetail.getModelId())) {
				sb.append(" and modelId = :modelId");
				param.setParams("modelId", logModelDetail.getModelId());
			}
			if (Validator.isNotNull(logModelDetail.getColumnCname())) {
				sb.append(" and columnCname = :columnCname");
				param.setParams("columnCname", logModelDetail.getColumnCname());
			}
			if (Validator.isNotNull(logModelDetail.getColumnEname())) {
				sb.append(" and columnEname = :columnEname");
				param.setParams("columnEname", logModelDetail.getColumnEname());
			}
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
